package com.egghiro.j2se.game.sace.model.actor.bullet;

import java.awt.Color;

import com.egghiro.framework.j2se.game.core.model.actor.IActor2D;
import com.egghiro.framework.j2se.game.core.view.SImage;
import com.egghiro.framework.j2se.game.model.element.LinearVelocity2D;
import com.egghiro.j2se.game.sace.model.actor.Flyer;
import com.egghiro.j2se.game.sace.param.SACEConst;
import com.egghiro.j2se.game.sace.state.actorstate.FlyerForwardState;
import com.egghiro.j2se.game.sace.state.actorstate.FlyerState;
import com.egghiro.j2se.game.sace.state.actorstate.StateManager;

/**
 * 静态折角弹
 *
 * @date 2012-6-3
 * @version 0.2
 */
public class StaticAngleBullet extends Bullet {
    private static final double DEFAULT_SPEED = 5;
    private static final SImage IMG = SImage.createImage(15, 8);
    static {
        IMG.getSGraphics().setColor(Color.ORANGE);
        IMG.getSGraphics().fillRect(0, 0, IMG.getWidth(), IMG.getHeight());
    }
//    private static final SImage IMG = SImage.createImage(PropertyUtils
//            .getAIMPropertyValue(SACEConst.AMI_P_Key_IMG_REIMU_FU));
    private StateManager<FlyerState> stateManager = new StateManager<FlyerState>();
    private static final int TURN_ANGLE = 105;

    public StaticAngleBullet(int x, int y, double angle, IActor2D target, long life) {
        this(x, y, DEFAULT_SPEED, angle, target, life);
    }

    public StaticAngleBullet(int x, int y, double speed, double angle, IActor2D target, long life) {
        this(x, y, new LinearVelocity2D(speed, angle), target, life);
    }

    public StaticAngleBullet(int x, int y, LinearVelocity2D speed, IActor2D target, long life) {
        super(x, y, StaticAngleBullet.IMG, target, life);
        stateManager.add(new FlyerForwardState(this, speed) {
            private long delay = 500;

            @Override
            public void countDown() {
                if ((delay -= SACEConst.SLEEP) <= 0) {
                    this.speed.turn(TURN_ANGLE);
                    getFlyer().setState(StaticAngleBullet.this.stateManager.pop());
                }
            }
        });
        stateManager.add(new ForwardAndTurnRightState(this, speed));
        stateManager.add(new ForwardAndTurnLeftState(this, speed));
        setState(stateManager.pop());
    }

    protected class ForwardAndTurnLeftState extends FlyerForwardState {
        private final long REDELAY = 800;
        private long delay;

        public ForwardAndTurnLeftState(Flyer flyer, LinearVelocity2D speed) {
            super(flyer, speed);
            reset();
        }

        private void reset() {
            this.delay = REDELAY;
        }

        @Override
        public void countDown() {
            if ((delay -= SACEConst.SLEEP) <= 0) {
                turn();
                getFlyer().setState(StaticAngleBullet.this.stateManager.pop());
                stateManager.add(this);
                reset();
            }
        }

        protected void turn() {
            this.speed.turn(TURN_ANGLE);
        }
    }

    protected class ForwardAndTurnRightState extends ForwardAndTurnLeftState {
        public ForwardAndTurnRightState(Flyer flyer, LinearVelocity2D speed) {
            super(flyer, speed);
        }

        @Override
        protected void turn() {
            this.speed.turn(-TURN_ANGLE);
        }

    }
}
